package com.ddtc.controller;

import com.ddtc.object.WxAppId;
import com.ddtc.object.WxAppIdAuth;
import com.ddtc.object.WxAutoReply;
import com.ddtc.repo.SourceInfoRepository;
import com.ddtc.repo.WxAppIdAuthRepository;
import com.ddtc.repo.WxAppIdRepository;
import com.ddtc.repo.WxAutoReplyRepository;
import lombok.extern.log4j.Log4j;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/**
 * Created by dingd on 2018-07-29.
 */
@RestController
@Log4j
public class ReplyController {

    @Autowired
    WxAppIdAuthRepository wxAppIdAuthRepo;

    @Autowired
    WxAutoReplyRepository wxAutoReplyRepo;

    @Autowired
    WxAppIdRepository wxAppIdRepo;

    @Autowired
    SourceInfoRepository sourceInfoRepo;

    @GetMapping("/getAutoReplyFirst")
    public ResponseEntity<?> getAutoReplyFirst(@RequestParam String appId,@RequestParam String index){
        //int i = Integer.valueOf("-"+index);
        String sessionFrom = index;
        Map<String, Object> resultMap = new HashMap<>();

        //resultMap.put("i",i);
        WxAppIdAuth result = wxAppIdAuthRepo.findByAppId(appId);

       WxAutoReply wxAutoReply =
                wxAutoReplyRepo.findByRoleAndSessionFrom(result.getAutoReplyRole(),sessionFrom);

        String source = sourceInfoRepo.findRequestSourceByAppId(appId);
        //String content = wxAutoReplyRepo.getContent(result.getAutoReplyRole(), sessionFrom);
        //String appSercet  = wxAppIdRepo.findAppSecret(appId);
        //resultMap.put("appSercet",appSercet);
        resultMap.put("accessToken",source);
        resultMap.put("content",wxAutoReply.getReply());
        return  new ResponseEntity<Map<String,Object>>(resultMap, HttpStatus.OK);
    }


    @GetMapping("/getAutoReplyContent")
    public ResponseEntity<?> getAutoReplyContent(@RequestParam String oldId,@RequestParam String keyWord){

        Map<String, Object> resultMap = new HashMap<>();

        WxAppId wxAppId  = wxAppIdRepo.findWxAppId(oldId);
        WxAppIdAuth result = wxAppIdAuthRepo.findByAppId(wxAppId.getAppId());


        String resultReply = wxAutoReplyRepo.getContent(result.getAutoReplyRole(), keyWord);

        if(resultReply == null){
            resultReply = wxAutoReplyRepo.getContent(result.getAutoReplyRole(), "NotFound");
        }
        resultMap.put("appId",wxAppId.getAppId());

        String source = sourceInfoRepo.findRequestSourceByAppId(wxAppId.getAppId());
        resultMap.put("accessToken",source);

        resultMap.put("content",resultReply);
        System.out.print(resultMap);
        return  new ResponseEntity<Map<String,Object>>(resultMap, HttpStatus.OK);
    }


    @GetMapping("/getAppId")
    public ResponseEntity<?> getAppId(){
        String appId = "*****";
        WxAppId appSercet  = wxAppIdRepo.findWxAppId("*****");
        return  new ResponseEntity<WxAppId>(appSercet, HttpStatus.OK);
    }

    @GetMapping("/getAll")
    public ResponseEntity<?> getAll(){
        Iterable<WxAutoReply> all = wxAutoReplyRepo.findAll();
        return  new ResponseEntity<Iterable<WxAutoReply>>(all, HttpStatus.OK);
    }

    @GetMapping("/getSource")
    public ResponseEntity<?> getSource(@RequestParam String appId){
        Map<String, Object> resultMap = new HashMap<>();

        String source = sourceInfoRepo.findRequestSourceByAppId(appId);
        resultMap.put("accessToken",source);
        return  new ResponseEntity<Map<String,Object>>(resultMap, HttpStatus.OK);
    }
}
